<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-col :span="12">
          <el-form-item label="供货人编号" prop="supId">
            <el-input
              v-model="queryParams.supId"
              placeholder="请输入供货人编号"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供货人名称" prop="supName">
            <el-input
              v-model="queryParams.supName"
              placeholder="请输入供货人名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="supState">
            <!-- <el-input
            v-model="queryParams.supState"
            placeholder="请输入状态"
            clearable
            @keyup.enter="handleQuery"
          /> -->
            <el-select
              v-model="queryParams.supState"
              placeholder="请输入状态"
              clearable
              @keyup.enter="handleQuery"
            >
              <el-option
                v-for="sta in status"
                :key="sta"
                :label="sta"
                :value="sta"
              />
            </el-select>
          </el-form-item> </el-col
        ><el-col :span="15">
          <el-form-item label="供应商类型" prop="supType">
            <!-- <el-input
            v-model="queryParams.supType"
            placeholder="请输入供应商类型"
            clearable
            @keyup.enter="handleQuery"
          /> -->
            <el-select
              v-model="queryParams.supType"
              placeholder="请输入供应商类型"
              clearable
              @keyup.enter="handleQuery"
            >
              <el-option
                v-for="type in types"
                :key="type"
                :label="type"
                :value="type"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form></el-row
    >

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 表格展示 -->
    <el-table
      v-loading="loading"
      :data="sups"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="供应商编号"
        width="100"
        align="center"
        prop="supId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应商名称"
        align="center"
        prop="supName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应商地址"
        align="center"
        prop="supAddress"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应商邮箱"
        align="center"
        prop="supEmail"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="supContact"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="supTel"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开户银行"
        align="center"
        prop="supBank"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开户行账号"
        align="center"
        prop="supAccount"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="supState"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应商类型"
        align="center"
        prop="supType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
          <!-- <el-tooltip content="执行一次" placement="top">
              <el-button
                type="text"
                icon="CaretRight"
                @click="handleRun(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="任务详细" placement="top">
              <el-button
                type="text"
                icon="View"
                @click="handleView(scope.row)"
                v-hasPermi="['monitor:job:query']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="调度日志" placement="top">
              <el-button
                type="text"
                icon="Operation"
                @click="handleJobLog(scope.row)"
                v-hasPermi="['monitor:job:query']"
              ></el-button>
            </el-tooltip> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改定时任务对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="jobRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供货人名称" prop="supName">
              <el-input v-model="form.supName" placeholder="请输入供货人名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商地址" prop="warehouseName">
              <el-input
                v-model="form.supAddress"
                placeholder="请输入供应商地址"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供货商邮箱" prop="supEmail">
              <el-input
                v-model="form.supEmail"
                placeholder="请输入供货商邮箱"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="supContact">
              <el-input v-model="form.supContact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人电话" prop="supTel">
              <el-input v-model="form.supTel" placeholder="请输入联系人电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="supBank">
              <el-input v-model="form.supBank" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户账号" prop="supAccount">
              <el-input
                v-model="form.supAccount"
                placeholder="请输入开户账号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="supState">
              <el-select
                v-model="form.supState"
                placeholder="请输入状态"
                clearable
                @keyup.enter="handleQuery"
              >
                <el-option
                  v-for="sta in status"
                  :key="sta"
                  :label="sta"
                  :value="sta"
              /></el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商类型" prop="supType">
              <el-select
                v-model="form.supType"
                placeholder="请输入供应商类型"
                clearable
                @keyup.enter="handleQuery"
              >
                <el-option
                  v-for="type in types"
                  :key="type"
                  :label="type"
                  :value="type"
              /></el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sup">
import {
  addSup,
  delSup,
  getSup,
  listSup,
  updateSup,
} from "@/api/purchase/sup.js";

// const router = useRouter();
const { proxy } = getCurrentInstance();
// const { sys_job_group, sys_job_status } = proxy.useDict(
//   "sys_job_group",
//   "sys_job_status"
// );

const sups = ref([]);
const status = ref(["正常", "失效"]);
const types = ref([
  "电子元件供应商",
  "塑料制品供应商",
  "包装材料供应商",
  "机械设备供应商",
  "办公用品供应商",
  "物流服务供应商",
  "广告服务供应商",
  "清洁服务供应商",
  "餐饮服务供应商",
  "技术服务供应商",
]);

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const openView = ref(false);
const openCron = ref(false);
const expression = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supId: undefined,
    supName: undefined,
    supAddress: undefined,
    supEmail: undefined,
    supContact: undefined,
    supTel: undefined,
    supBank: undefined,
    supAccount: undefined,
    supState: undefined,
    supType: undefined,
  },
  rules: {
    supName: [{ required: true, message: "请输入供货人名称", trigger: "blur" }],
    supAddress: [
      { required: true, message: "请输入供应商地址", trigger: "blur" },
    ],
    supEmail: [
      { required: true, message: "请输入供货商邮箱", trigger: "blur" },
      { type: "email", message: "邮箱地址格式不正确", trigger: "blur" },
    ],
    supContact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
    supTel: [
      { required: true, message: "请输入联系人电话", trigger: "blur" },
      {
        pattern: /^1[3-9]\d{9}$/,
        message: "电话号码格式不正确",
        trigger: "blur",
      },
    ],
    supBank: [{ required: true, message: "请输入开户银行", trigger: "blur" }],
    supAccount: [
      { required: true, message: "请输入开户账号", trigger: "blur" },
      { pattern: /^\d+$/, message: "开户账号只能包含数字", trigger: "blur" },
    ],
    supState: [{ required: true, message: "请选择状态", trigger: "change" }],
    supType: [
      { required: true, message: "请选择供应商类型", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询定时任务列表 */
function getList() {
  loading.value = true;
  listSup(queryParams.value).then((response) => {
    console.log(response);
    loading.value = false;
    sups.value = response.data.records;
    total.value = response.data.total;
  });
}
// //  /** 任务组名字典翻译 */
// //  function jobGroupFormat(row, column) {
// //     return proxy.selectDictLabel(sys_job_group.value, row.jobGroup);
// //  }
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    warehouseName: undefined,
    warehouseLocation: undefined,
    warehouseResponsible: undefined,
    //  jobId: undefined,
    //  jobName: undefined,
    //  jobGroup: undefined,
    //  invokeTarget: undefined,
    //  cronExpression: undefined,
    //  misfirePolicy: 1,
    concurrent: 1,
    status: "0",
  };
  proxy.resetForm("jobRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.supId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
// //  // 更多操作触发
// //  function handleCommand(command, row) {
// //     switch (command) {
// //        case "handleRun":
// //           handleRun(row);
// //           break;
// //        case "handleView":
// //           handleView(row);
// //           break;
// //        case "handleJobLog":
// //           handleJobLog(row);
// //           break;
// //        default:
// //           break;
// //     }
// //  }
// //  // 任务状态修改
// //  function handleStatusChange(row) {
// //     let text = row.status === "0" ? "启用" : "停用";
// //     proxy.$modal.confirm('确认要"' + text + '""' + row.jobName + '"任务吗?').then(function () {
// //        return changeJobStatus(row.jobId, row.status);
// //     }).then(() => {
// //        proxy.$modal.msgSuccess(text + "成功");
// //     }).catch(function () {
// //        row.status = row.status === "0" ? "1" : "0";
// //     });
// //  }
// //  /* 立即执行一次 */
// //  function handleRun(row) {
// //     proxy.$modal.confirm('确认要立即执行一次"' + row.jobName + '"任务吗?').then(function () {
// //        return runJob(row.jobId, row.jobGroup);
// //     }).then(() => {
// //        proxy.$modal.msgSuccess("执行成功");
// //     })
// //        .catch(() => { });
// //  }
// //  /** 任务详细信息 */
// //  function handleView(row) {
// //     getJob(row.jobId).then(response => {
// //        form.value = response.data;
// //        openView.value = true;
// //     });
// //  }
// //  /** cron表达式按钮操作 */
// //  function handleShowCron() {
// //     expression.value = form.value.cronExpression;
// //     openCron.value = true;
// //  }
// //  /** 确定后回传值 */
// //  function crontabFill(value) {
// //     form.value.cronExpression = value;
// //  }
// //  /** 任务日志列表查询 */
// //  function handleJobLog(row) {
// //     const jobId = row.jobId || 0;
// //     router.push({ path: "/monitor/job-log/index", query: { jobId: jobId } });
// //  }
// /** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供货商信息";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const supId = row.supId || ids.value;
  getSup(supId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改供货商信息";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["jobRef"].validate((valid) => {
    if (valid) {
      if (form.value.supId !== undefined) {
        updateSup(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSup(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const supId = row.supId || ids.value;
  proxy.$modal
    .confirm('是否确认删除供货商编号为"' + supId + '"的数据项?')
    .then(function () {
      return delSup(supId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// //  /** 导出按钮操作 */
// //  function handleExport() {
// //     proxy.download("monitor/job/export", {
// //        ...queryParams.value,
// //     }, `job_${new Date().getTime()}.xlsx`);
// //  }

getList();
</script>
